#ifndef CFG_V3_H
#define CFG_V3_H

#include "dist.h"
#include <stdlib.h>

#define N3_HR_SD_THRESHOLD 4

#define MIN_JUDGMENT_SLEEP_TIME_2 20
#define SEGMENTED_DATA_CALCULATION_TIME                                        \
  (MIN_SD_DATA_SIZE * get_smp_interval() + 1)
// 同v4兼容
#define SEGMENTED_CALCULATION_TIME SEGMENTED_DATA_CALCULATION_TIME
#define MIN_AMOUNT_OF_SLEEPING_CONDITIONS                                      \
  (MIN_JUDGMENT_SLEEP_TIME / SEGMENTED_DATA_CALCULATION_TIME + 1)
#define SLEEP_HR_AVG_THRESHOLD 100
#define FIND_SLEEP_WAKE_MOTION_THRESHOLD (get_smp_interval() * 15)
#define SLEEP_WAKE_STEPS_DIFF_THRESHOLD (get_smp_interval() * 6)

#define SLEEP_WAKE_MOTION_THRESHOLD (get_smp_interval() * 30)
#define N1_MOTION_THRESHOLD (get_smp_interval() * 4)
#define SLEEP_MOTION_AVG_THRESHOLD (get_smp_interval() * 20)
#define FIND_SLEEP_MOTION_THRESHOLD (get_smp_interval() * 4)
#define FIND_WAKE_MOTION_THRESHOLD (get_smp_interval() * 10)
#define FIND_SLEEP_WAKE_STEPS_DIFF_THRESHOLD (get_smp_interval() * 4)
#define N3_MOTION_THRESHOLD (get_smp_interval() * 1)

size_t raw_start_end(size_t begin, size_t end, const seg_info_t *results,
                     size_t len_result, size_t *index_end);

#define SLEEP_START_END_CAL_NEW()                                              \
  if ((end - begin + 1 >= MIN_AMOUNT_OF_SLEEPING_CONDITIONS)) {                \
    index_start =                                                              \
        raw_start_end(begin, end, seg_results, len_result, &index_end);        \
    size_t out_start, out_end;                                                 \
    if (_dist->find_sleep_start_end_impl(1, hr_list, count, index_start,       \
                                         index_end, &out_start, &out_end)) {   \
      sleep_start_end_t *obj =                                                 \
          (sleep_start_end_t *)malloc(sizeof(sleep_start_end_t));              \
      obj->start_index = out_start;                                            \
      obj->end_index = out_end;                                                \
      append_item(sleep_list, obj, NULL);                                      \
    }                                                                          \
  }

#define POST_CALL_START_END_IMPL() free_items(v4_list, NULL, 0, NULL);

#define EXTRA_STAGING_TYPE()                                                   \
  return motion_avg <= N1_MOTION_THRESHOLD &&                                  \
                 steps_diff <= NREM1_REM_STEPS_DIFF_THRESHOLD                  \
             ? ENUM_SLEEP_STAGING_TYPE_NREM1                                   \
             : ENUM_SLEEP_STAGING_TYPE_REM;

#endif